<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">




  
  
  
  

  
    
    
  

  
    
      
    

    
  

  

  

  
    
      
    

    
  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic|Roboto Slab:300,300italic,400,400italic,700,700italic|PT Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="几种实现双向数据绑定做法目前几种主流的mvc(vm)框架都实现了单向数据绑定，而我所理解的双向数据绑定无非就是在单向绑定的基础上给可输入元素（input、textare等）添加了change(input)事件，来动态修改model和 view，并没有多高深。所以无需太过介怀是实现的单向或双向绑定。 实现数据绑定的做法有大致如下几种：  发布者-订阅者模式（backbone.js） 脏值检查（ang">
<meta property="og:type" content="article">
<meta property="og:title" content="vue双向数据绑定">
<meta property="og:url" content="http://blog.xiaowuzi.info/2016/05/08/vue双向数据绑定/index.html">
<meta property="og:site_name" content="小武子博客">
<meta property="og:description" content="几种实现双向数据绑定做法目前几种主流的mvc(vm)框架都实现了单向数据绑定，而我所理解的双向数据绑定无非就是在单向绑定的基础上给可输入元素（input、textare等）添加了change(input)事件，来动态修改model和 view，并没有多高深。所以无需太过介怀是实现的单向或双向绑定。 实现数据绑定的做法有大致如下几种：  发布者-订阅者模式（backbone.js） 脏值检查（ang">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://blog.xiaowuzi.info/images/vue/132184689-57b310ea1804f_articlex.png">
<meta property="og:image" content="http://blog.xiaowuzi.info/images/vue/1117380429-57b3110440af0.png">
<meta property="og:updated_time" content="2018-04-15T10:41:06.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="vue双向数据绑定">
<meta name="twitter:description" content="几种实现双向数据绑定做法目前几种主流的mvc(vm)框架都实现了单向数据绑定，而我所理解的双向数据绑定无非就是在单向绑定的基础上给可输入元素（input、textare等）添加了change(input)事件，来动态修改model和 view，并没有多高深。所以无需太过介怀是实现的单向或双向绑定。 实现数据绑定的做法有大致如下几种：  发布者-订阅者模式（backbone.js） 脏值检查（ang">
<meta name="twitter:image" content="http://blog.xiaowuzi.info/images/vue/132184689-57b310ea1804f_articlex.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://blog.xiaowuzi.info/2016/05/08/vue双向数据绑定/">





  <title>vue双向数据绑定 | 小武子博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">小武子博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.xiaowuzi.info/2016/05/08/vue双向数据绑定/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tony">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小武子博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">vue双向数据绑定</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-05-08T10:57:02+08:00">
                2016-05-08
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/javascript框架/" itemprop="url" rel="index">
                    <span itemprop="name">javascript框架</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="几种实现双向数据绑定做法"><a href="#几种实现双向数据绑定做法" class="headerlink" title="几种实现双向数据绑定做法"></a>几种实现双向数据绑定做法</h3><p>目前几种主流的mvc(vm)框架都实现了单向数据绑定，而我所理解的双向数据绑定无非就是在单向绑定的基础上给可输入元素（input、textare等）添加了change(input)事件，来动态修改model和 view，并没有多高深。所以无需太过介怀是实现的单向或双向绑定。</p>
<p>实现数据绑定的做法有大致如下几种：</p>
<blockquote>
<p>发布者-订阅者模式（backbone.js）</p>
<p>脏值检查（angular.js） </p>
<p>数据劫持（vue.js）</p>
</blockquote>
<p><strong>发布者-订阅者模式:</strong> 一般通过sub, pub的方式实现数据和视图的绑定监听，更新数据方式通常做法是 vm.set(‘property’, value)，这里有篇文章讲的比较详细，有兴趣可点<a href="http://www.html-js.com/article/Study-of-twoway-data-binding-JavaScript-talk-about-JavaScript-every-day" target="_blank" rel="noopener">这里</a></p>
<p>这种方式现在毕竟太low了，我们更希望通过 vm.property = value 这种方式更新数据，同时自动更新视图，于是有了下面两种方式<br><a id="more"></a></p>
<p><strong>脏值检查:</strong>angular.js 是通过脏值检测的方式比对数据是否有变更，来决定是否更新视图，最简单的方式就是通过 setInterval() 定时轮询检测数据变动，当然Google不会这么low，angular只有在指定的事件触发时进入脏值检测，大致如下：</p>
<ul>
<li>DOM事件，譬如用户输入文本，点击按钮等。( ng-click )</li>
<li>XHR响应事件 ( $http )</li>
<li>浏览器Location变更事件 ( $location )</li>
<li>Timer事件( $timeout , $interval )</li>
<li>执行 $digest() 或 $apply()</li>
</ul>
<p><strong>数据劫持:</strong> vue.js 则是采用数据劫持结合发布者-订阅者模式的方式，通过Object.defineProperty()来劫持各个属性的setter，getter，在数据变动时发布消息给订阅者，触发相应的监听回调。</p>
<h3 id="重新认识Object-defineProperty"><a href="#重新认识Object-defineProperty" class="headerlink" title="重新认识Object.defineProperty()"></a>重新认识Object.defineProperty()</h3><p>Object.defineProperty() 方法会直接在一个对象上定义一个新属性，或者修改一个对象的现有属性， 并返回这个对象。</p>
<h4 id="参数"><a href="#参数" class="headerlink" title="参数"></a>参数</h4><p><code>obj</code> 要在其上定义属性。</p>
<p><code>prop</code>要定义或修改的属性的名称</p>
<p><code>descriptor</code> 将被定义或个性的属性描述符。</p>
<p>属性描述符具有以下可选键值：</p>
<p><code>configurable</code>当且仅当该属性的configurable为true时，该属性描述符才能被改变，同时该属性也能从对应的对象上被删除。默认为false.</p>
<p><code>enumerable</code></p>
<p>数据描述符同时具有以下可选键值：</p>
<p><code>value</code>该属性对应的值。可以是任何有效的 JavaScript 值（数值，对象，函数等）。默认为 undefined。</p>
<p><code>writable</code>当且仅当该属性的writable为true时，value才能被赋值运算符改变。默认为 false。</p>
<p>存取描述符同时具有以下可选键值：</p>
<p><code>get</code>一个给属性提供 getter 的方法，如果没有 getter 则为 undefined。该方法返回值被用作属性值。默认为 undefined。</p>
<p><code>set</code>一个给属性提供 setter 的方法，如果没有 setter 则为 undefined。该方法将接受唯一参数，并将该参数的新值分配给该属性。默认为 undefined。</p>
<p>示例：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">var o = &#123;&#125;; // 创建一个新对象</span><br><span class="line"></span><br><span class="line">// 在对象中添加一个属性与数据描述符的示例</span><br><span class="line">Object.defineProperty(o, &quot;a&quot;, &#123;</span><br><span class="line">  value : 37,</span><br><span class="line">  writable : true,</span><br><span class="line">  enumerable : true,</span><br><span class="line">  configurable : true</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">//对象o拥有了属性a，值为37</span><br></pre></td></tr></table></figure>
<p>关于<code>Object.defineProperty()</code><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty" target="_blank" rel="noopener">详细使用请点击这里阅读更多用法</a>。</p>
<h3 id="思路整理"><a href="#思路整理" class="headerlink" title="思路整理"></a>思路整理</h3><p>要实现mvvm的双向绑定，就必须要实现以下几点：</p>
<p>实现一个数据监听器Observer，能够对数据对象的所有属性进行监听，如有变动可拿到最新值并通知订阅者<br>实现一个指令解析器Compile，对每个元素节点的指令进行扫描和解析，根据指令模板替换以及绑定相应的更新函数。<br>实现一个Watcher，作为连接Observer和Compile的桥梁，能够订阅并收到每个属性变动的通知，执行指令绑定的相应回调函数，从而更新视图。<br>mvvm入口函数，整合以上三者<br>上述流程如图所示：<br><img src="/images/vue/132184689-57b310ea1804f_articlex.png" alt=""></p>
<h3 id="实现Observer"><a href="#实现Observer" class="headerlink" title="实现Observer"></a>实现Observer</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">var data=&#123;name:&apos;tony&apos;&#125;;</span><br><span class="line">observe(data);</span><br><span class="line">data.name=&apos;xiaowuzi&apos;;</span><br><span class="line"></span><br><span class="line">function observe(data)&#123;</span><br><span class="line">	if(!data || typeof data!==&apos;object&apos;) return;</span><br><span class="line">	//取出所有属性遍历</span><br><span class="line">	Object.keys(data).forEach(function(key)&#123;</span><br><span class="line">		defineReactive(data,key,data[key]);</span><br><span class="line">	&#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function defineReactive(data,key,val)&#123;</span><br><span class="line">	observe(val);	//监听子属性</span><br><span class="line">	Object.defineProperty(data,key,&#123;</span><br><span class="line">		enumerable:true,		//可枚举</span><br><span class="line">		configurable:false,	//不能再define</span><br><span class="line">		get:function()&#123;</span><br><span class="line">			return val;</span><br><span class="line">		&#125;,</span><br><span class="line">		set:function(newVal)&#123;</span><br><span class="line">			console.log(&apos;监听到值的变化了&apos;,val,&apos;--&gt;&apos;,newVal);</span><br><span class="line">			val=newVal;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;);</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这样我们已经可以监听每个数据的变化了，那么监听到变化之后就是怎么通知订阅者了，所以接下来我们需要实现一个消息订阅器，很简单，维护一个数组，用来收集订阅者，数据变动触发notify，再调用订阅者的update方法，代码改善之后是这样：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">var data=&#123;name:&apos;tony&apos;&#125;;</span><br><span class="line">observe(data);</span><br><span class="line">data.name=&apos;xiaowuzi&apos;;</span><br><span class="line"></span><br><span class="line">function observe(data)&#123;</span><br><span class="line">	if(!data || typeof data!==&apos;object&apos;) return;</span><br><span class="line">	//取出所有属性遍历</span><br><span class="line">	Object.keys(data).forEach(function(key)&#123;</span><br><span class="line">		defineReactive(data,key,data[key]);</span><br><span class="line">	&#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function defineReactive(data,key,val)&#123;</span><br><span class="line">	observe(val);	//监听子属性</span><br><span class="line">	Object.defineProperty(data,key,&#123;</span><br><span class="line">		enumerable:true,		//可枚举</span><br><span class="line">		configurable:false,	//不能再define</span><br><span class="line">		get:function()&#123;</span><br><span class="line">			return val;</span><br><span class="line">		&#125;,</span><br><span class="line">		set:function(newVal)&#123;</span><br><span class="line">			console.log(&apos;监听到值的变化了&apos;,val,&apos;--&gt;&apos;,newVal);</span><br><span class="line">			val=newVal;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;);</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function Dep()&#123;</span><br><span class="line">	this.subs=[];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Dep.prototype=&#123;</span><br><span class="line">	addSub:function(sub)&#123;</span><br><span class="line">		this.subs.push(sub);</span><br><span class="line">	&#125;,</span><br><span class="line">	notify:function()&#123;</span><br><span class="line">		this.subs.forEach(function(sub)&#123;</span><br><span class="line">			sub.update();</span><br><span class="line">		&#125;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>上面的思路整理中我们已经明确订阅者应该是Watcher, 而且<code>var dep = new Dep();</code>是在 defineReactive方法内部定义的，所以想通过dep添加订阅者，就必须要在闭包内操作，所以我们可以在 getter里面动手脚：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">var data=&#123;name:&apos;tony&apos;&#125;;</span><br><span class="line">observe(data);</span><br><span class="line">data.name=&apos;xiaowuzi&apos;;</span><br><span class="line"></span><br><span class="line">function observe(data)&#123;</span><br><span class="line">	if(!data || typeof data!==&apos;object&apos;) return;</span><br><span class="line">	//取出所有属性遍历</span><br><span class="line">	Object.keys(data).forEach(function(key)&#123;</span><br><span class="line">		defineReactive(data,key,data[key]);</span><br><span class="line">	&#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function defineReactive(data,key,val)&#123;</span><br><span class="line">	observe(val);	//监听子属性</span><br><span class="line">	Object.defineProperty(data,key,&#123;</span><br><span class="line">		enumerable:true,		//可枚举</span><br><span class="line">		configurable:false,	//不能再define</span><br><span class="line">		get:function()&#123;</span><br><span class="line">			//由于需要在装饰内添加watcher，所以通过Dep定义一个全局target属性，暂存watcher，添加完移除</span><br><span class="line">			Dep.target &amp;&amp; dep.addDep(Dep.target);</span><br><span class="line">			return val;</span><br><span class="line">		&#125;,</span><br><span class="line">		set:function(newVal)&#123;</span><br><span class="line">			console.log(&apos;监听到值的变化了&apos;,val,&apos;--&gt;&apos;,newVal);</span><br><span class="line">			val=newVal;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;);</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function Dep()&#123;</span><br><span class="line">	this.subs=[];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Dep.prototype=&#123;</span><br><span class="line">	addSub:function(sub)&#123;</span><br><span class="line">		this.subs.push(sub);</span><br><span class="line">	&#125;,</span><br><span class="line">	notify:function()&#123;</span><br><span class="line">		this.subs.forEach(function(sub)&#123;</span><br><span class="line">			sub.update();</span><br><span class="line">		&#125;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>这里已经实现了一个Observer了，已经具备了监听数据和数据变化通知订阅者的功能。</p>
<h3 id="2-实现Compile"><a href="#2-实现Compile" class="headerlink" title="2.实现Compile"></a>2.实现Compile</h3><p>compile主要做的事情是解析模板指令，将模板中的变量替换成数据，然后初始化渲染页面视图，并将每个指令对应的节点绑定更新函数，添加监听数据的订阅者，一旦数据有变动，收到通知，更新视图，如图所示：</p>
<p><img src="/images/vue/1117380429-57b3110440af0.png" alt=""></p>
<p>因为遍历解析的过程有多次操作dom节点，为提高性能和效率，会先将跟节点el转换成文档碎片fragment进行解析编译操作，解析完成，再将fragment添加回原来的真实dom节点中</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">function Compile(el)&#123;</span><br><span class="line">	this.$el=this.isElementNode(el)?el:document.querySelector(el);</span><br><span class="line">	if(this.$el)&#123;</span><br><span class="line">		this.$fragment=this.node2Fragment(this.$el);</span><br><span class="line">		this.init();</span><br><span class="line">		this.$el.appendChild(this.$fragment);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">Compile.prototype=&#123;</span><br><span class="line">	init:function()&#123;this.compileElement(this.$fragment);&#125;,</span><br><span class="line">	node2Fragment:function(el)&#123;</span><br><span class="line">		var fragment=document.createDocumentFragment(),child;</span><br><span class="line">		//将原生节点拷贝到fragment</span><br><span class="line">		while(child==el.firstChild)&#123;</span><br><span class="line">			fragment.appendChild(child);</span><br><span class="line">		&#125;</span><br><span class="line">		return;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>compileElement方法将遍历所有节点及其子节点，进行扫描解析编译，调用对应的指令渲染函数进行数据渲染，并调用对应的指令更新函数进行绑定，详看代码及注释说明：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line">function Compile(el)&#123;</span><br><span class="line">	this.$el=this.isElementNode(el)?el:document.querySelector(el);</span><br><span class="line">	if(this.$el)&#123;</span><br><span class="line">		this.$fragment=this.node2Fragment(this.$el);</span><br><span class="line">		this.init();</span><br><span class="line">		this.$el.appendChild(this.$fragment);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">Compile.prototype=&#123;</span><br><span class="line">	init:function()&#123;this.compileElement(this.$fragment);&#125;,</span><br><span class="line">	node2Fragment:function(el)&#123;</span><br><span class="line">		var fragment=document.createDocumentFragment(),child;</span><br><span class="line">		//将原生节点拷贝到fragment</span><br><span class="line">		while(child==el.firstChild)&#123;</span><br><span class="line">			fragment.appendChild(child);</span><br><span class="line">		&#125;</span><br><span class="line">		return;</span><br><span class="line">	&#125;</span><br><span class="line">	compileElement:function(el)&#123;</span><br><span class="line">		var childNodes=el.childNodes,me=this;</span><br><span class="line">	[].slice.call(childNodes).forEach(function(node)&#123;</span><br><span class="line">			var text=node.textContent;</span><br><span class="line">			var reg=/\&#123;(.*)\&#125;\&#125;/;	//表达式文本</span><br><span class="line">			//按元素节点方式编译</span><br><span class="line">			if(me.isElementNode(node))&#123;</span><br><span class="line">				me.compile(node);</span><br><span class="line">			&#125;else if(me.isTextNode(node) &amp;&amp; reg.test(text))&#123;</span><br><span class="line">				me.compileText(node,RegExp.$1);</span><br><span class="line">			&#125;</span><br><span class="line">			//遍历编译子节点</span><br><span class="line">			if(node.childNodes &amp;&amp; node.childNodes.length)&#123;</span><br><span class="line">				me.compileElement(node);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;);</span><br><span class="line">	&#125;,</span><br><span class="line">	compile:function(node)&#123;</span><br><span class="line">		var nodeAttrs=node.attributes,me=this;</span><br><span class="line">		[].slice.call(nodeAttrs).forEach(function(attr)&#123;</span><br><span class="line">		//规定：指令v-xx命名</span><br><span class="line">		//如&lt;span v-text=&quot;content&quot;&gt;&lt;/span&gt;中指令为v-text</span><br><span class="line">			var attrName=attr.name;	//v-text</span><br><span class="line">			if(me.isDirective(attrName))&#123;</span><br><span class="line">				var exp=attr.value;</span><br><span class="line">				var dir=attrName.substring(2);</span><br><span class="line">				if(me.isEventDirective(dir))&#123;</span><br><span class="line">					//事件指令，如v-on:click</span><br><span class="line">					compileUtil.eventHandler(node,me.$vm,exp,dir);</span><br><span class="line">				&#125;else&#123;</span><br><span class="line">					//普通指令</span><br><span class="line">					compileUtil[dir] &amp;&amp; compileUtil[dir](node,me.$vm,exp);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">//指令处理集合</span><br><span class="line">var compileUtil=&#123;</span><br><span class="line">	text:function(node,vm,exp)&#123;</span><br><span class="line">		this.bind(node,vm,exp,&apos;text&apos;);</span><br><span class="line">	&#125;,</span><br><span class="line">	bind:function(node,vm,exp,dir)&#123;</span><br><span class="line">		var updateFn=updater[dir+&apos;Updater&apos;];</span><br><span class="line">		//第一次初始化视图</span><br><span class="line">		updateFn &amp;&amp; updateFn(node,vm[exp]);</span><br><span class="line">		//实例化订阅者，此操作会在对应的属性消息订阅器中添加了该订阅者watcher</span><br><span class="line">		new Watcher(vm,exp,function(value,oldVale)&#123;</span><br><span class="line">			//一旦属性值有变化，会收到通知执行此更新函数，更新视图</span><br><span class="line">			updateFn &amp;&amp; updateFn(node,value,oldValue);</span><br><span class="line">		&#125;);</span><br><span class="line">	&#125;,</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">//更新函数</span><br><span class="line">var updater=&#123;</span><br><span class="line">	textUpdater:function(node,value)&#123;</span><br><span class="line">		node.textContent=typeof value==&apos;undefined&apos;?&apos;&apos;:value;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这里通过递归遍历保证了每个节点及子节点都会解析编译到，包括了双括号表达式声明的文本节点。指令的声明规定是通过特定前缀的节点属性来标记，如&lt;span v-text=”content” other-attr中v-text便是指令，而other-attr不是指令，只是普通的属性。<br>监听数据、绑定更新函数的处理是在compileUtil.bind()这个方法中，通过new Watcher()添加回调来接收数据变化的通知</p>
<h3 id="3-实现Watcher"><a href="#3-实现Watcher" class="headerlink" title="3.实现Watcher"></a>3.实现Watcher</h3><p>Watcher订阅者作为Observer和Compile之间通信的桥梁，主要做的事情是:</p>
<ol>
<li>在自身实例化时往属性订阅器(dep)里面添加自己</li>
<li>自身必须有一个update()方法</li>
<li>待属性变动dep.notice()通知时，能调用自身的update()方法，并触发Compile中绑定的回调，则功成身退。</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">function Watcher(vm,exp,cb)&#123;</span><br><span class="line">	this.cb=cb;</span><br><span class="line">	this.vm=vm;</span><br><span class="line">	this.exp=exp;</span><br><span class="line">	//此处为了触发属性的getter，从而在dep添加自己，结合Observer更易理解</span><br><span class="line">	this.value=this.get();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Watcher.prototype=&#123;</span><br><span class="line">	update:function()&#123;</span><br><span class="line">		this.run();	//属性值变化收到通知</span><br><span class="line">	&#125;,</span><br><span class="line">	run:function()&#123;</span><br><span class="line">		var value=this.get(); // 取到最新值</span><br><span class="line">		var oldVal=this.value;</span><br><span class="line">		if(value!==oldVal)&#123;</span><br><span class="line">			this.value=value;</span><br><span class="line">			this.cb.call(this.vm,value,oldVal); // 执行Compile中绑定的回调，更新视图</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;,</span><br><span class="line">	get:function()&#123;</span><br><span class="line">		Dep.target=this;// 将当前订阅者指向自己</span><br><span class="line">		var value=this.vm[exp];// 触发getter，添加自己到属性订阅器中</span><br><span class="line">		Dep.target=null; // 添加完毕，重置</span><br><span class="line">		return value;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>实例化Watcher的时候，调用get()方法，通过Dep.target = watcherInstance标记订阅者是当前watcher实例，强行触发属性定义的getter方法，getter方法执行的时候，就会在属性的订阅器dep添加当前watcher实例，从而在属性值有变化的时候，watcherInstance就能收到更新通知。</p>
<h3 id="4、实现MVVM"><a href="#4、实现MVVM" class="headerlink" title="4、实现MVVM"></a>4、实现MVVM</h3><p>MVVM作为数据绑定的入口，整合Observer、Compile和Watcher三者，通过Observer来监听自己的model数据变化，通过Compile来解析编译模板指令，最终利用Watcher搭起Observer和Compile之间的通信桥梁，达到数据变化 -&gt; 视图更新；视图交互变化(input) -&gt; 数据model变更的双向绑定效果。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">function MVVM(options)&#123;</span><br><span class="line">	this.$options=options;</span><br><span class="line">	var data=this._data=this.$options.data;</span><br><span class="line">	observe(data,this);</span><br><span class="line">	this.$compile=new Compile(optiosn.el || document.body,this);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>但是这里有个问题，从代码中可看出监听的数据对象是options.data，每次需要更新视图，则必须通过<code>var vm = new MVVM({data:{name: &#39;kindeng&#39;}}); vm._data.name = &#39;dmq&#39;;</code> 这样的方式来改变数据。</p>
<p>改造后的代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">function MVVM(options)&#123;</span><br><span class="line">	this.$options=options;</span><br><span class="line">	var data=this._data=this.$options.data,me=this;</span><br><span class="line">	//属性代理，实现 vm.xxx-&gt;vm._data.xxx</span><br><span class="line">	Object.keys(data).forEach(function(key)&#123;</span><br><span class="line">		me._proxy(key);</span><br><span class="line">	&#125;);</span><br><span class="line">	observe(data,this);</span><br><span class="line">	this.$compile=new Compile(options.el || document.body,this);</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line">MVVM.prototye=&#123;</span><br><span class="line">	_proxy:function(key)&#123;</span><br><span class="line">		var me=this;</span><br><span class="line">		_proxy:function(key)&#123;</span><br><span class="line">			configurable:false,</span><br><span class="line">			enumerable:true,</span><br><span class="line">			get:function proxyGetter()&#123;</span><br><span class="line">				return me._data[key];</span><br><span class="line">			&#125;,</span><br><span class="line">			set:function proxySetter(newVal)&#123;</span><br><span class="line">				me._data[key]=newVal;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>参考资料：</p>
<p><a href="http://www.cnblogs.com/kidney/p/6052935.html" target="_blank" rel="noopener">Vue.js双向绑定的实现原理</a></p>
<p><a href="https://segmentfault.com/a/1190000006599500" target="_blank" rel="noopener">剖析Vue原理&amp;实现双向绑定MVVM</a></p>
<p><a href="https://github.com/DDFE/DDFE-blog/issues/7" target="_blank" rel="noopener">深入响应式原理</a></p>
      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center">
  <div>Donate comment here</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.jpg" alt="Tony 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpg" alt="Tony 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/05/07/跨域/" rel="next" title="跨域">
                <i class="fa fa-chevron-left"></i> 跨域
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/07/08/http2/" rel="prev" title="http2">
                http2 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Tony</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">65</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">18</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#几种实现双向数据绑定做法"><span class="nav-number">1.</span> <span class="nav-text">几种实现双向数据绑定做法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#重新认识Object-defineProperty"><span class="nav-number">2.</span> <span class="nav-text">重新认识Object.defineProperty()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#参数"><span class="nav-number">2.1.</span> <span class="nav-text">参数</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#思路整理"><span class="nav-number">3.</span> <span class="nav-text">思路整理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实现Observer"><span class="nav-number">4.</span> <span class="nav-text">实现Observer</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-实现Compile"><span class="nav-number">5.</span> <span class="nav-text">2.实现Compile</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-实现Watcher"><span class="nav-number">6.</span> <span class="nav-text">3.实现Watcher</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4、实现MVVM"><span class="nav-number">7.</span> <span class="nav-text">4、实现MVVM</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 &mdash; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Tony</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  



  
  









  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three-waves.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>